<?php
class UserPaypalConnection extends AppModel
{
    public $name = 'UserPaypalConnection';
    public $actsAs = array(
        'Aggregatable'
    );
    //$validate set in __construct for multi-language support
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'counterCache' => true
        )
    );
    public $hasMany = array(
        'ProjectFund' => array(
            'className' => 'ProjectFund',
            'foreignKey' => 'user_paypal_connection_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );
    var $aggregatingFields = array(
        'charged_amount' => array(
            'mode' => 'real',
            'key' => 'user_paypal_connection_id',
            'foreignKey' => 'user_paypal_connection_id',
            'model' => 'ProjectFund',
            'function' => 'SUM(ProjectFund.amount)',
            'conditions' => array(
                'ProjectFund.is_paid' => 1,
                'ProjectFund.is_failed' => 0,
                'ProjectFund.is_refunded' => 0,
            )
        ) ,
        'charged_count' => array(
            'mode' => 'real',
            'key' => 'user_paypal_connection_id',
            'foreignKey' => 'user_paypal_connection_id',
            'model' => 'ProjectFund',
            'function' => 'COUNT(ProjectFund.user_paypal_connection_id)',
            'conditions' => array(
                'ProjectFund.is_paid' => 1,
                'ProjectFund.is_failed' => 0,
                'ProjectFund.is_refunded' => 0,
            )
        ) ,
    );
    function __construct($id = false, $table = null, $ds = null)
    {
        parent::__construct($id, $table, $ds);
        $this->validate = array(
            'user_id' => array(
                'rule' => 'numeric',
                'allowEmpty' => false,
                'message' => __l('Required')
            ) ,
            'pre_approval_key' => array(
                'rule' => 'notempty',
                'allowEmpty' => false,
                'message' => __l('Required')
            ) ,
        );
    }
}
?>